﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using DataTransferObject;
using Presentation.Infrastructure;

namespace Presentation
{
    public class ReportPanneViewModel : ViewModelBase
    {
        private bool _isLoading;
        private bool _isDialogVisible;
        private DateTime _observationDate;
        private string _comments;
        private ObjetPatrimonialDTO _selectedObjetPatrimonial;
        private PanneDTO _selectedPanne;
        private readonly ObservableCollection<ObjetPatrimonialDTO> _objetPatrimonialCollection;
        private readonly ObservableCollection<PanneDTO> _panneCollection; 
        
        private readonly SimpleCommand _createSignalementCommand;
        private readonly SimpleCommand _confirmCreateSignalementCommand;
        private readonly SimpleCommand _cancelCommand;

        public bool IsLoading
        {
            get { return _isLoading; }
            set
            {
                _isLoading = value;
                this.NotifyPropertyChanged(o => o.IsLoading);
                RefreshCommands();
            }
        }

        public ObjetPatrimonialDTO SelectedObjetPatrimonial
        {
            get { return _selectedObjetPatrimonial; }
            set
            {
                _selectedObjetPatrimonial = value;
                this.NotifyPropertyChanged(o => o.SelectedObjetPatrimonial);
            }
        }

        public PanneDTO SelectedPanne
        {
            get { return _selectedPanne; }
            set
            {
                _selectedPanne = value;
                this.NotifyPropertyChanged(o => o.SelectedPanne);
            }
        }

        public ObservableCollection<ObjetPatrimonialDTO> ObjetPatrimonialCollection
        {
            get { return _objetPatrimonialCollection; }
        }

        public ObservableCollection<PanneDTO> PanneCollection
        {
            get { return _panneCollection; }
        }

        public  string Comments
        {
            get { return _comments; }
            set
            {
                _comments = value;
                this.NotifyPropertyChanged(o => o.Comments);
            }
        }

        public bool IsDialogVisible
        {
            get { return _isDialogVisible; }
            set
            {
                _isDialogVisible = value;
                this.NotifyPropertyChanged(o => o.IsDialogVisible);
            }
        }

        public DateTime ObservationDate
        {
            get { return _observationDate; }
            set
            {
                _observationDate = value;
                this.NotifyPropertyChanged(o => o.ObservationDate);
            }
        }

        public SimpleCommand CreateSignalementCommand
        {
            get { return _createSignalementCommand; }
        }

        public SimpleCommand ConfirmCreateSignalementCommand
        {
            get { return _confirmCreateSignalementCommand; }
        }

        public SimpleCommand CancelCommand
        {
            get { return _cancelCommand; }
        }

        public ReportPanneViewModel()
        {
            Title = "Créer un signalement";
            CurrentUser = ProxyClient.CurrentUser;
            ObservationDate = DateTime.Now;

            _objetPatrimonialCollection = new ObservableCollection<ObjetPatrimonialDTO>();
            _panneCollection = new ObservableCollection<PanneDTO>();

            _createSignalementCommand = new SimpleCommand(OnCreateSignalementCommandExecuted);
            _confirmCreateSignalementCommand = new SimpleCommand(OnConfirmCreateSignalementCommandExecuted);
            _cancelCommand = new SimpleCommand(OnCancelCommandExecuted);

            LoadReportingPanneDatasAsync();
        }

        private void LoadReportingPanneDatasAsync()
        {
            IsLoading = true;

            ProxyClient.Client.GetCreateSignalementDatasCompleted += (sender, args) =>
            {
                var reportingPannesDatas = args.Result;
                if (reportingPannesDatas != null)
                {
                    if (reportingPannesDatas.ObjetPatrimonials != null)
                        _objetPatrimonialCollection.AddRangeAsync(reportingPannesDatas.ObjetPatrimonials);
                    if (reportingPannesDatas.Pannes != null)
                        _panneCollection.AddRangeAsync(reportingPannesDatas.Pannes);
                }
                IsLoading = false;
            };

            ProxyClient.Client.GetCreateSignalementDatasAsync();
        }

        protected override void RefreshCommands()
        {
            _createSignalementCommand.RaiseCanExecuteChanged();
            _cancelCommand.RaiseCanExecuteChanged();
        }

        private void OnCreateSignalementCommandExecuted(object param)
        {
            IsDialogVisible = true;
        }

        private void OnConfirmCreateSignalementCommandExecuted(object param)
        {
            var pannePerDateDetailDTO = new List<SignalementGroupDetailDTO>()
                                            {
                                                new SignalementGroupDetailDTO
                                                    {
                                                        ObjetPatrimonialId = SelectedObjetPatrimonial.Id,
                                                        PanneId = SelectedPanne.Id,
                                                        Comments = Comments
                                                    }
                                            };

            var signalementGroupDTO = new SignalementGroupDTO
                                       {ObservationDate = ObservationDate, Details = pannePerDateDetailDTO};

            ProxyClient.Client.CreateManySignalement(signalementGroupDTO);

            IsDialogVisible = false;

            NavigationService.Instance.NavigateTo(typeof(DashboardView), true);
        }

        private  void OnCancelCommandExecuted(object param)
        {
            NavigationService.Instance.NavigateTo(typeof(DashboardView));
        }
    }
}
